package com.space.base;

import io.swagger.annotations.ApiModelProperty;

/**
 * @author zhaixianhao
 */
public class Page extends BasePO {
    private static final Integer DEFAULT_PAGE_SIZE = 15;
    @ApiModelProperty(
            value = "当前页 默认 1",
            example = "1"
    )
    private Integer pageNumber;
    @ApiModelProperty(
            value = "页面大小 默认 15",
            example = "15"
    )
    private Integer pageSize;

    public Page() {
        this(1, DEFAULT_PAGE_SIZE);
    }

    public Page(Integer pageNumber, Integer pageSize) {
        this.pageNumber = pageNumber;
        this.pageSize = Math.max(pageSize, 0);
    }

    protected Page(final Page.PageBuilder<?, ?> b) {
        super(b);
        this.pageNumber = b.pageNumber;
        this.pageSize = b.pageSize;
    }

    public static Page.PageBuilder<?, ?> builder() {
        return new Page.PageBuilderImpl();
    }

    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Page)) {
            return false;
        } else {
            Page other = (Page)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (!super.equals(o)) {
                return false;
            } else {
                Object this$pageNumber = this.getPageNumber();
                Object other$pageNumber = other.getPageNumber();
                if (this$pageNumber == null) {
                    if (other$pageNumber != null) {
                        return false;
                    }
                } else if (!this$pageNumber.equals(other$pageNumber)) {
                    return false;
                }

                Object this$pageSize = this.getPageSize();
                Object other$pageSize = other.getPageSize();
                if (this$pageSize == null) {
                    if (other$pageSize != null) {
                        return false;
                    }
                } else if (!this$pageSize.equals(other$pageSize)) {
                    return false;
                }

                return true;
            }
        }
    }

    @Override
    protected boolean canEqual(final Object other) {
        return other instanceof Page;
    }

    @Override
    public int hashCode() {
        boolean PRIME = true;
        int result = super.hashCode();
        Object $pageNumber = this.getPageNumber();
        result = result * 59 + ($pageNumber == null ? 43 : $pageNumber.hashCode());
        Object $pageSize = this.getPageSize();
        result = result * 59 + ($pageSize == null ? 43 : $pageSize.hashCode());
        return result;
    }

    public Integer getPageNumber() {
        return this.pageNumber;
    }

    public Integer getPageSize() {
        return this.pageSize;
    }

    public void setPageNumber(final Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public void setPageSize(final Integer pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public String toString() {
        return "Page(pageNumber=" + this.getPageNumber() + ", pageSize=" + this.getPageSize() + ")";
    }

    private static final class PageBuilderImpl extends Page.PageBuilder<Page, Page.PageBuilderImpl> {
        private PageBuilderImpl() {
        }

        @Override
        protected Page.PageBuilderImpl self() {
            return this;
        }

        @Override
        public Page build() {
            return new Page(this);
        }
    }

    public abstract static class PageBuilder<C extends Page, B extends Page.PageBuilder<C, B>> extends BasePOBuilder<C, B> {
        private Integer pageNumber;
        private Integer pageSize;

        public PageBuilder() {
        }

        @Override
        protected abstract B self();

        @Override
        public abstract C build();

        public B pageNumber(final Integer pageNumber) {
            this.pageNumber = pageNumber;
            return this.self();
        }

        public B pageSize(final Integer pageSize) {
            this.pageSize = pageSize;
            return this.self();
        }

        @Override
        public String toString() {
            return "Page.PageBuilder(super=" + super.toString() + ", pageNumber=" + this.pageNumber + ", pageSize=" + this.pageSize + ")";
        }
    }
}
